import Vue from "vue";
import VueRouter from "vue-router";
import axios from "axios";
import VueAxios from "vue-axios";
import Vuex from "vuex";
import router from "./router.js";
import app from "./App.vue";

import Mint from 'mint-ui';
import moment from "moment";
import VuePreview from 'vue-preview';

import "mint-ui/lib/style.css";
import "./lib/mui/css/mui.min.css";
import "./lib/mui/css/icons-extra.css";
import "./lib/mui/fonts/mui-icons-extra.ttf";

//按需引入mint-ui
// import { Header, Swipe, SwipeItem , Button , Lazyload} from "mint-ui";
// Vue.component(Header.name, Header);
// Vue.component(Swipe.name, Swipe);
// Vue.component(SwipeItem.name, SwipeItem);
// Vue.component(Button.name, Button);
// Vue.use(Lazyload);


Vue.use(VueRouter);
Vue.use(VueAxios, axios);
Vue.use(Mint);
// defalut install
Vue.use(VuePreview);
Vue.use(Vuex);
let car = JSON.parse(window.localStorage.getItem('car') || '[]');
var store = new Vuex.Store({
    state: {
        cart: car
    },
    mutations: {
        //追加购物车商品
        addToCar(state, goodsInfo) {
            // 追加商品之前查看是否有该商品
            // 如果有则修改追加数量
            let flag = false;
            state.cart.some(item => {
                if (item.id == goodsInfo.id) {
                    item.count += parseInt(goodsInfo.count);
                    flag = true;
                    return true;
                }
            })
            //如果没有则追加到购物车中
            if (!flag) {
                state.cart.push(goodsInfo);
            }
            localStorage.setItem("car",JSON.stringify(state.cart));
        },
        //修改购物车中的商品数量
        updateGoodsInfo(state,goodsInfo){
            state.cart.some(item=>{
                if(item.id == goodsInfo.id){
                    item.count = parseInt(goodsInfo.count);
                    return true;
                }
            })
            localStorage.setItem("car",JSON.stringify(state.cart));
        },
        //删除购物城中的商品
        removeGoodsInfo(state,goodsid){
            state.cart.some((item,i)=>{
                if(item.id == goodsid){
                    state.cart.splice(i,1);
                    return true;
                }
            })
            localStorage.setItem("car",JSON.stringify(state.cart));
        }
    },
    getters: {
        getGoodsCount(state){
            let count = 0;
            state.cart.forEach(item=>{
                count+=item.count;
            })
            return count;
        },
        getCountObj(state){
            let o = {};
            state.cart.forEach(item=>{
                o[item.id] = item.count;
            })
            return o;
        },
        getGoodsSelected(state){
            let o = {};
            state.cart.forEach(item=>{
                o[item.id] = item.selected;
            })
            return o;
        }
    }
})

Vue.filter("dateFormat", function (dataStr, formatStr = "YYYY-MM-DD HH:mm:ss") {
    return moment(dataStr).format(formatStr);
})

axios.defaults.baseURL = "http://www.liulongbin.top:3005/";

var vm = new Vue({
    el: "#app",
    render: c => c(app),
    router,
    store
})